package com.example.dell.fastshopping.user.address;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.network.http.BaseResponse;
import com.example.dell.fastshopping.views.BaseListActivity;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ${YSH} on 2019/4/24.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */
public class AddressManagerActivity extends BaseListActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.add_address)
    TextView addAddress;
    @BindView(R.id.nodata)
    LinearLayout nodata;
    @BindView(R.id.refresh)
    TwinklingRefreshLayout refresh;
    private AddressViewMolder molder;
    private String from;
    private List<MyAddress> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        ButterKnife.bind(this);
        super.initData();
        initView();
    }

    @Override
    protected void getNetData(final int page, int pageSize) {
        molder.getAddressList().observe(this, new Observer<List<MyAddress>>() {
            @Override
            public void onChanged(@Nullable List<MyAddress> baseResponse) {
                if (baseResponse == null) {
                    return;
                }
                if (page == 1) {
                    list = baseResponse;
                } else {
                    list.addAll(baseResponse);
                }

                if (list.size() == 0) {
                    nodata.setVisibility(View.VISIBLE);
                } else {
                    nodata.setVisibility(View.GONE);
                }
                loadFinsh();
                RecyclerView.Adapter adapter = rv.getAdapter();
                if (adapter == null) {
                    rv.setAdapter(new MyAdapter());
                } else {
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    protected TwinklingRefreshLayout getRefreshLayout() {
        return refresh;
    }

    private void initView() {
        title.setText("地址管理");
        Intent intent = getIntent();
        from = intent.getStringExtra("from");//订单过来需要返回数据
        molder = ViewModelProviders.of(this).get(AddressViewMolder.class);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setBackgroundColor(getResources().getColor(R.color.pager_grey));
//        rv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh.startRefresh();
    }

    @OnClick({R.id.add_address, R.id.back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.add_address:
                Intent intent = new Intent(this, AddAddressActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void toAddAddressActivity(MyAddress address) {
        Intent intent = new Intent(this, AddAddressActivity.class);
        intent.putExtra("entity", address);
        startActivity(intent);
    }

    private class MyAdapter extends RecyclerView.Adapter<MyHoder> {

        @NonNull
        @Override
        public MyHoder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View inflate = LayoutInflater.from(AddressManagerActivity.this).inflate(R.layout.item_address, viewGroup, false);
            MyHoder myHoder = new MyHoder(inflate);
            return myHoder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyHoder myHoder, int i) {
            MyAddress myAddress = list.get(i);
            if ("order".equals(from)){
                myHoder.itemView.setOnClickListener(myHoder);
            }
            myHoder.delete.setOnClickListener(myHoder);
            myHoder.edit.setOnClickListener(myHoder);
            myHoder.status.setOnClickListener(myHoder);
            myHoder.name.setText(myAddress.getUserName());
            myHoder.phone.setText(myAddress.getPhone());
            myHoder.address.setText(myAddress.getArea() + myAddress.getAddress());
            myHoder.setPosition(i);
            int stauts = myAddress.getDefaults();//0：普通 1 :默认

            if (stauts == 0) {
                myHoder.status.setImageDrawable(AddressManagerActivity.this.getResources().getDrawable(R.mipmap.unselect));
            } else {
                myHoder.status.setImageDrawable(AddressManagerActivity.this.getResources().getDrawable(R.mipmap.select));
            }
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    private class MyHoder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView name;
        public TextView phone;
        public TextView address;
        public ImageView status;
        public TextView edit;
        public TextView delete;
        private int mPosition;

        public MyHoder(@NonNull View itemView) {
            super(itemView);
            delete = itemView.findViewById(R.id.delete);
            name = itemView.findViewById(R.id.name);
            phone = itemView.findViewById(R.id.phone);
            address = itemView.findViewById(R.id.address);
            status = itemView.findViewById(R.id.status);
            edit = itemView.findViewById(R.id.edit);
        }

        @Override
        public void onClick(View v) {
            MyAddress myAddress = list.get(mPosition);
            switch (v.getId()) {
                case R.id.delete:

                    deleteAddress(myAddress);
                    break;
                case R.id.status:
                    if (list.size() != 1) {

                        int defaults = myAddress.getDefaults();
                        if (defaults == 0) {
                            myAddress.setDefaults(1);
                        } else {
                            myAddress.setDefaults(0);
                        }
                        editAddress(myAddress);
                    }
                    break;
                case R.id.edit:
                    toAddAddressActivity(myAddress);
                    break;
                case R.id.item_address:
                    back(myAddress);;
                    break;
            }
        }

        public void setPosition(int position) {
            this.mPosition = position;
        }
    }

    private void back(MyAddress myAddress) {
        Intent intent = new Intent();
        intent.putExtra("entity",myAddress);
        setResult(RESULT_OK,intent);
        finish();
    }


    private void deleteAddress(MyAddress myAddress) {
        molder.deleteAddress(myAddress).observe(this, new Observer<BaseResponse>() {
            @Override
            public void onChanged(@Nullable BaseResponse baseResponse) {
                if (baseResponse.isSuccess()) {
                    refresh.startRefresh();
                }
            }
        });

    }


    private void editAddress(MyAddress myAddress) {

        molder.editAddress(myAddress).observe(this, new Observer<BaseResponse>() {
            @Override
            public void onChanged(@Nullable BaseResponse baseResponse) {
                if (baseResponse.isSuccess()) {
                    refresh.startRefresh();
                }
            }
        });
    }
}
